package oppsa.view;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

import oppsa.controller.FormatData;
import oppsa.controller.Translate;
import oppsa.model.UserDB;

import org.jdesktop.swingx.JXFrame;

public class ProfilView  extends JXFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JPasswordField tfPass1;
	private JPasswordField tfPass2;
	private JTextField tfMail;
	private JPasswordField tfOldPass;
	private Translate t = new Translate();
	private HomeView hv;
	
	public ProfilView(HomeView homeView) {
		
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (ClassNotFoundException e1) {
	
			System.err.println("Ops : "+ e1.getMessage());
		} catch (InstantiationException e1) {
	
			System.err.println("Ops : "+ e1.getMessage());
		} catch (IllegalAccessException e1) {
	
			System.err.println("Ops : "+ e1.getMessage());
		} catch (UnsupportedLookAndFeelException e1) {
	
			System.err.println("Ops : "+ e1.getMessage());
		}
		this.hv= homeView;
		
		addWindowListener(closeWindow);
		hv.addWindowFocusListener(clickhomeview);
		setIconImage(Toolkit.getDefaultToolkit().getImage(HomeView.class.getResource("/LOGO_FONCE.png")));
		setBounds(100, 100, 450, 308);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblPass1 = new JLabel(t.getLocalizedText("lblPass1"));
		lblPass1.setBounds(10, 81, 179, 14);
		contentPane.add(lblPass1);
		
		JLabel lblPass2 = new JLabel(t.getLocalizedText("lblPass2"));
		lblPass2.setBounds(10, 112, 179, 14);
		contentPane.add(lblPass2);
		
		tfPass1 = new JPasswordField();
		tfPass1.setBounds(222, 78, 155, 24);
		contentPane.add(tfPass1);
		tfPass1.setColumns(10);
		
		tfPass2 = new JPasswordField();
		tfPass2.setBounds(222, 109, 155, 24);
		contentPane.add(tfPass2);
		tfPass2.setColumns(10);
		
		JButton btnSubmitPass = new JButton(t.getLocalizedText("btnSubmitPass"));
		btnSubmitPass.setBounds(222, 149, 171, 23);
		contentPane.add(btnSubmitPass);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(10, 183, 414, 2);
		contentPane.add(separator);
		
		JLabel lblMailProfil = new JLabel(t.getLocalizedText("lblSubmitMail"));
		lblMailProfil.setBounds(10, 196, 179, 14);
		contentPane.add(lblMailProfil);
		
		tfMail = new JTextField();
		tfMail.setBounds(222, 193, 155, 24);
		contentPane.add(tfMail);
		tfMail.setColumns(10);
		
		JButton btnSubmitMail = new JButton(t.getLocalizedText("btnSubmitMail"));
		btnSubmitMail.setBounds(222, 224, 171, 23);
		contentPane.add(btnSubmitMail);
		
		JLabel lblProfil = new JLabel(t.getLocalizedText("lblProfil"));
		lblProfil.setBounds(76, 11, 262, 14);
		contentPane.add(lblProfil);
		
		JLabel lblOldPass = new JLabel(t.getLocalizedText("lblOlPass"));
		lblOldPass.setBounds(10, 50, 179, 14);
		contentPane.add(lblOldPass);
		
		tfOldPass = new JPasswordField();
		tfOldPass.setBounds(222, 47, 155, 24);
		contentPane.add(tfOldPass);
		tfOldPass.setColumns(10);

		
		btnSubmitMail.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub

				hv.setEnabled(true);
				setVisible(false);		
			}
			
		});
		
	}
	WindowFocusListener  clickhomeview = new  WindowFocusListener () {

		@Override
		public void windowGainedFocus(WindowEvent arg0) {
			// TODO Auto-generated method stub
			toFront();
		}

		@Override
		public void windowLostFocus(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}
	};
	
	ActionListener  changeMail = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			UserDB user = new UserDB();
			if(FormatData.isMail(tfMail.getText())){
				user.updateMail(tfMail.getText());
			} else {
				JOptionPane.showMessageDialog(ProfilView.this,t.getLocalizedText("lblWrongMailFormat"),t.getLocalizedText("lblMail"),JOptionPane.INFORMATION_MESSAGE);
			}
		}
	};
	ActionListener  changePassword = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			UserDB user = new UserDB();
			if(tfPass1.getPassword().equals(tfPass2.getPassword())){
				if(user.updatePassword(tfOldPass.getPassword(), tfPass1.getPassword())){
					
				}
			} else {
				JOptionPane.showMessageDialog(ProfilView.this,t.getLocalizedText("lblSamePw"),t.getLocalizedText("lblPassword"),JOptionPane.INFORMATION_MESSAGE);
			}
		}
	};
	WindowAdapter closeWindow = new WindowAdapter() {
		@Override
		public void windowClosing(WindowEvent arg0) {
			
			setVisible(false);
			hv.setVisible(true);
			hv.setEnabled(true);
			hv.toFront();
		}
	};
}
